from manim import *


class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(60*2+1):       
            dot1=Dot([-500,5*(i-60),0]).set_opacity(0.5)
            dot2=Dot([500,5*(i-60),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(100*2+1):        
            dot3=Dot([(i-100)*5,-300,0]).set_opacity(0.5)
            dot4=Dot([(i-100)*5,300,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        
        #镜头跟进效果 
        self.camera.frame.save_state()

        

        text = Tex(r"\text{目标函数}$f\left( x_{1},x_{2},\ldots x_{n}\right) $",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([0,0,0])
        text1 = Tex(r"\text{约束方程}$\varphi \left( x_{1},x_{2},\ldots x_{n}\right) $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([0,0,0])
        self.play(Write(text))
        self.play(text.animate().scale(0.5).move_to([-20,20,0])) 
        self.play(Write(text1))
        self.play(text1.animate().scale(0.5).move_to([20,20,0])) 
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)

        text2 = Tex(r"\text{均值不等式}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,15,0])
        text3 = Tex(r"$\sqrt[n] {x_{1}x_{2}\ldots x_{n}}\leq \dfrac{x_{1}+x_{2}+\ldots +x_{n}}{n}\leq \sqrt{\dfrac{x_{1}^{2}+x_{2}^{2}+\ldots +x_{n}^{2}}{n}} $",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,5,0])
        self.play(Create(text2),Create(text3))
        
        text4 = Tex(r"\text{柯西不等式}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-5,0])
        text5 = Tex(r"$ \left( x_{1}^{2}+x_{2}^{2}+\ldots +x_{n}^{2}\right) \left( y_{1}^{2}+y_{2}^{2}+\ldots +y_{n}^{2}\right) \geq \left( x_{1}y_{1}+x_{2}y_{2}+...+x_{n}y_{n}\right) ^{2}$",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-15,0])
        self.play(Create(text4),Create(text5))

        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=15,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([-26, 5, 0])
        self.play(Create(rectangle))
        
        text6 = Tex(r"\text{特征1:乘积}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle,UP,buff=1)
        self.play(Create(text6))

        rectangle2=Rectangle(color=BLUE,fill_opacity=0.0,width=20,height=10
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle2.move_to([-5, 5, 0])
        self.play(Create(rectangle2))
        
        text8 = Tex(r"\text{特征2:一次和式}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle2,UP,buff=1)
        self.play(Create(text8))

        rectangle3=Rectangle(color=BLUE,fill_opacity=0.0,width=22,height=10
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle3.move_to([21, 5, 0])
        self.play(Create(rectangle3))
        
        text9 = Tex(r"\text{特征3:平方和式}",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle3,UP,buff=1)
        self.play(Create(text9))
        

        rectangle1=Rectangle(color=BLUE,fill_opacity=0.0,width=21,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle1.move_to([-5.5, -15, 0])
        self.play(Create(rectangle1))
        
        text7 = Tex(r"\text{系数调节因子}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle1,UP,buff=1)
        self.play(Create(text7))


        rectangle4=Rectangle(color=BLUE,fill_opacity=0.0,width=23,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle4.move_to([-29, -15, 0])
        self.play(Create(rectangle4))

        text10 = Tex(r"\text{特征：平方和式}",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle4,UP,buff=1)
        self.play(Create(text10))



        rectangle5=Rectangle(color=BLUE,fill_opacity=0.0,width=30,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle5.move_to([25, -15, 0])
        self.play(Create(rectangle5))
        
        text11 = Tex(r"\text{特征：一次和式}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle5,UP,buff=1)
        self.play(Create(text11))


        



        self.wait()